package condominios.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;


import condominios.excepcion.DAOExcepcion;
import condominios.modelo.Inscripcion;
import condominios.util.ConexionBD;


public class InscripcionDAO extends BaseDAO{
	
	public Inscripcion insertar(Inscripcion ins) throws DAOExcepcion {
		
		String query = "INSERT INTO residentes(nombre,DNI,fec_nac,correo,clave) VALUES (?,?,?,?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, ins.getNombre());
			stmt.setString(2, ins.getDNI());
			stmt.setString(3, ins.getFecnac());
			stmt.setString(4, ins.getcorreo());
			stmt.setString(5, ins.getPassword());
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
			// Obtener el ultimo id
			int id = 0;
			query = "SELECT LAST_INSERT_ID()";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			if (rs.next()) {
				id = rs.getInt(1);
			}
			ins.setidResidente(id);
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return ins;
	}
	public Inscripcion obtener(int idResidente) throws DAOExcepcion {
		Inscripcion ins = new Inscripcion();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String query = "select idresidentes, nombre, dni, fec_nac, correo, clave from residentes where idresidentes=?";
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idResidente);
			rs = stmt.executeQuery();
			if (rs.next()) {
				ins.setidResidente(rs.getInt(1));
				ins.setNombre(rs.getString(2));
				ins.setDNI(rs.getString(3));
				ins.setFecnac(rs.getString(4));
				ins.setcorreo(rs.getString(5));
				ins.setPassword(rs.getString(6));
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return ins;
	}

	public Collection<Inscripcion> listar() throws DAOExcepcion {
		System.out.println("RolDAO: listar()");
		Collection<Inscripcion> c = new ArrayList<Inscripcion>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			String query = "SELECT rol_nombre,descripcion from rol";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Inscripcion vo = new Inscripcion();
				vo.setNombre(rs.getString("nombre"));
				vo.setDNI(rs.getString("DNI"));
				vo.setFecnac(rs.getString("Fecnac"));
				vo.setcorreo(rs.getString("corrreo"));
				vo.setPassword(rs.getString("Password"));
				
				c.add(vo);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}

}
